/*
 * base64.h
 *
 *  Created on: 2008-12-17
 *      Author: fanliangliang
 */

#ifndef BASE64_H_
#define BASE64_H_

#include "config.h"
#include <cstdlib> // for size_t
#include <string>

BEGIN_NAMESPACE_TOXIC

using std::string;

class Base64 {
public:
	Base64();
	~Base64();

	string encode(const string& str);
	string encode(const void* input, size_t length);
	char* encode(const void* input, size_t inputLength, size_t* outputLength);

	string decode(const string& str);
	string decode(const void* input, size_t length);
	char* decode(const void* input, size_t inputLength, size_t* outputLength);

	void setAlphabet(const char* alphabet);
	void setPadding(char padding);

	const char* alphabet() const;
	char padding() const;

	void printAlphabet() const;
	void printReverseAlphabet() const;

private:
	size_t indexOf(char ch, const char* alphabet);
	void destroyReverseAlphabet();
	bool checkDecodeInput(const char* input, size_t length);
	char* encodeImplement(const char* input, size_t inputLength, size_t* outputLength);
	char* decodeImplement(const char* input, size_t inputLength, size_t* outputLength);

private:
	static const char DEFAULT_PADDING;
	static const char DEFAULT_ALPHABET[64];
	static const char DEFAULT_REVERSE_ALPHABET[128];

private:
	const char* _alphabet;
	const char* _reverseAlphabet;
	char _padding;
};

END_NAMESPACE_TOXIC

#endif /* BASE64_H_ */
